package com.ps.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ps.pojo.Role;
import com.ps.pojo.User;
import com.ps.service.UserService;
import com.ps.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-12-18
 */
@RestController
@RequestMapping("xt/yhgl")
public class UserController {
    @Resource
    private UserService userService;
    @GetMapping("/getAll")
    public Result getAll(Integer page, Integer limit, User user){
        Page<User>userPage=userService.getAll(page, limit, user);
        return Result.success(userPage);
    }
    @PostMapping("/add")
    public Result add(User user ,String bb) throws ParseException {
        user.setPassword("123456");
        user.setEntrydate(LocalDateTime.now());
        user.setCreationtime(LocalDateTime.now());
        user.setCreatedby("创建者");
        user.setUserstatus(1);

        /**
         * 计算年龄
         */
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        Calendar ca=Calendar.getInstance();
//        ca.setTime(sdf.parse(LocalDate.now().toString()));
//        Calendar ca1=Calendar.getInstance();
//        ca1.setTime(sdf.parse(bb));
//        int y=ca.get(ca.YEAR);
//        int y1=ca1.get(ca1.YEAR);
        user.setAge(LocalDate.parse(bb));

        boolean flag=userService.add(user);
        Result result=null;
        if (flag){
            result=result.success("添加成功！");
        }else {
            result=result.fail("添加失败！");
        }
        return result;
    }
    @PostMapping("/del")
    @ResponseBody
    public Result delById(Integer id){
        boolean flag=userService.removeById(id);
        Result result=null;
        if (flag){
            result=result.success("删除成功！");
        }else {
            result=result.fail("删除失败！");
        }
        return result;
    }
    @PostMapping("/update")
    @ResponseBody
    public Result updateRole(User user,String bb){
        user.setLastupdatetime(LocalDateTime.now());
        user.setAge(LocalDate.parse(bb));
        System.out.println(user);
        boolean flag=userService.updateById(user);
        Result result=null;
        if (flag){
            result=result.success("修改成功！");

        }else {
            result=result.fail("修改失败！");
        }
        return result;
    }
    @PostMapping("/delete")
    @ResponseBody
    public Result delet(Integer[]ids){
        boolean flag=userService.delet(ids);
        Result result=null;
        if (flag){
            result=result.success("删除成功！");

        }else {
            result=result.fail("删除失败！");
        }
        return result;
    }
}

